package com.yy.controller;

import com.yy.pojo.User;
import com.yy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//跟用户有关的操作都放在这里
@Controller
public class UserController {
    //    //登录
//    @RequestMapping("/login")
//    public String  login(HttpServletRequest request, HttpServletResponse response){
//        String username = request.getParameter("username");
//        String password = request.getParameter("password");
//        System.out.println("username = " + username);
//        System.out.println("password = " + password);
//        return "success";
//    }
//    @RequestMapping("/login")
//    public String  login(String username,String password){
//        System.out.println("username = " + username);
//        System.out.println("password = " + password);
//        return "success";
//    }
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(User user, HttpSession session) {
        System.out.println("username = " + user.getUsername());
        System.out.println("password = " + user.getPassword());
        User user1 = userService.login(user);
        if (user1 != null) {
            //登陆成功,用户信息放到session中
            session.setAttribute("user", user1);
//            return "success";
            return "redirect:/getAllGoods";
        } else {
            session.setAttribute("msg", "用户名或密码错误");
            System.out.println("用户名或者密码错误=========");
            //TODO
            return "redirect:/login.jsp";
        }
    }

    //注册
    @RequestMapping("/register")
    public String register(User user,HttpSession session){
        System.out.println("user = " + user);
        String nickname = user.getNickname();
        //如果昵称存在，直接重定向到 注册页面
        //否则，实现注册功能
        boolean flag=userService.nicknameExit(nickname);
        if(flag){
            //存在
            session.setAttribute("nickname",nickname);
            session.setAttribute("msg","此昵称已存在");
            return "redirect:/login.jsp";
        }else{
            //实现注册
            //TODO
            //新增成功，重定向到 登录页面；新增失败，跳转到注册页面
            boolean isAdd=userService.addUser(user);
            if(isAdd){
//                session.setAttribute("msg", "注册成功");
                return "redirect:/msg.jsp";
            }else{
                session.setAttribute("msg", "注册失败");
                return "redirect:/register.jsp";
            }
        }
    }
    @ResponseBody
    @RequestMapping("/hasNickname")
    public String hasNiacname(String nickname){
        System.out.println("nickname = " + nickname);
        //TODO l连接数据库 判断昵称是否存在
        /**
         * 测试后发现 进行了页面跳转  不想要
         * 我们想要把字符串 true 返回给 ajax 的回调函数
         * 只要添加一个注解@ResponseBody
         * 添加完注解后，直接返回结果，不走视图解析器
         */
        return "true";
    }
}
